package views;
/**
 * EDCTreeViewer
 * Viewer for tree part of DTD file viewer
 */
import javax.swing.JScrollPane;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import utils.EDCResource;


import java.awt.Dimension;
import java.awt.GridLayout;

public class EDCTreeViewer extends EDCDefaultViewer
implements TreeSelectionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	// private JEditorPane htmlPane;
	private JTree tree;
	private static boolean DEBUG = false;

	// Optionally play with line styles. Possible values are
	// "Angled" (the default), "Horizontal", and "None".
	private static boolean playWithLineStyle = false;
	private static String lineStyle = "Horizontal";

	public EDCTreeViewer(String filename) {
		setTitle(EDCResource.DTD_VIEWER);
		getContentPane().setLayout(new GridLayout());

		// Create the nodes.
		DefaultMutableTreeNode top = new DefaultMutableTreeNode(
		"Test");
		createNodes(top);

		// Create a tree that allows one selection at a time.
		tree = new JTree(top);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		// Listen for when the selection changes.
		tree.addTreeSelectionListener(this);

		if (playWithLineStyle) {
			System.out.println("line style = " + lineStyle);
			tree.putClientProperty("JTree.lineStyle", lineStyle);
		}

		// Create the scroll pane and add the tree to it.
		JScrollPane treeView = new JScrollPane(tree);
		Dimension minimumSize = new Dimension(100, 50);
		treeView.setMinimumSize(minimumSize);
		getContentPane().add(treeView);
	}

	/** Required by TreeSelectionListener interface. */
	public void valueChanged(TreeSelectionEvent e) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
		.getLastSelectedPathComponent();

		if (node == null)
			return;

		Object nodeInfo = node.getUserObject();
		if (node.isLeaf()) {
			if (DEBUG) {
//				System.out.print();
			}
		} else {
			// 
		}
		if (DEBUG) {
			System.out.println(nodeInfo.toString());
		}
	}

	private class DTDNode {
		public String dtdName;

		public DTDNode(String name, String other) {
			dtdName = name;
		}

		public String toString() {
			return dtdName;
		}
	}

	private void createNodes(DefaultMutableTreeNode top) {
		
		DefaultMutableTreeNode category = null;
		DefaultMutableTreeNode dtd = null;

		category = new DefaultMutableTreeNode("DTD catagory1");
		top.add(category);

		dtd = new DefaultMutableTreeNode(new DTDNode(
				"Node 1",
		""));
		category.add(dtd);


		category = new DefaultMutableTreeNode("DTD catagory2");
		top.add(category);

		// VM
		dtd = new DefaultMutableTreeNode(new DTDNode(
				"Node 2", ""));
		category.add(dtd);

	}

}
